//
//  LGStutaesFrame.m
//  微博
//
//  Created by MS on 15-12-16.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGStutaesFrame.h"

@implementation LGStutaesFrame

-(void)setStatues:(LGStatus *)statues
{
    _statues = statues;
    
    [self setUpOriginalViewFrame];
    
    CGFloat toolBarY = CGRectGetMaxY(_originalViewFrame);
    
    if (statues.retweeted_status) {
        [self setUpRetweetViewFrame];
        toolBarY = CGRectGetMaxY(_retSweetViewFrame);
    }
    
    // 计算工具条
    CGFloat toolBarX = 0;
    CGFloat toolBarW = LGScreenW;
    CGFloat toolBarH = 35;
    _ToolBarFrame = CGRectMake(toolBarX, toolBarY, toolBarW, toolBarH);
    
    // 计算cell高度
    _cellHight = CGRectGetMaxY(_ToolBarFrame);
    
}

#pragma mark - 计算原创微博
-(void)setUpOriginalViewFrame
{
    //头像
    CGFloat imageX = LGStatusCellMargin;
    CGFloat imageY =imageX;
    CGFloat imageWH =35;
    _originaIconFrame = CGRectMake(imageX, imageY, imageWH, imageWH);
   
     //呢称
    CGFloat nameX =CGRectGetMaxX(_originaIconFrame)+LGStatusCellMargin;
    CGFloat nameY = imageY;
    CGSize nameSize = [_statues.user.name sizeWithFont:LGNameFont];
    _originaNameFrame =(CGRect){{nameX,nameY},nameSize};

   //Vip
    if (_statues.user.vip) {
        CGFloat vipX = CGRectGetMaxX(_originaNameFrame) + LGStatusCellMargin;
        CGFloat vipY = nameY;
        CGFloat vipWH = 14;
        _originaVipFrame= CGRectMake(vipX, vipY, vipWH, vipWH);
        
    }

  
    //正文
    CGFloat textX = imageX;
    CGFloat textY = CGRectGetMaxY(_originaIconFrame) + LGStatusCellMargin;
    
    CGFloat textW =LGScreenW - 2 * LGStatusCellMargin;
    CGSize textSize = [_statues.text sizeWithFont:LGTextFont constrainedToSize:CGSizeMake(textW, MAXFLOAT)];
    _originaContentFrame = (CGRect){{textX,textY},textSize};

    //配图Frame
    CGFloat originH = CGRectGetMaxY(_originaContentFrame) + LGStatusCellMargin;
    if (_statues.pic_urls.count) {
        CGFloat photosViewX = LGStatusCellMargin;
        CGFloat photosViewY = CGRectGetMaxY(_originaContentFrame) + LGStatusCellMargin;
        CGSize photoViewSize = [self setPhotosSizeWithCount:_statues.pic_urls.count];
        _originaPhontosFrame = (CGRect){{photosViewX,photosViewY},photoViewSize};
        originH = CGRectGetMaxY(_originaPhontosFrame) + LGStatusCellMargin;
    }
   
    
    // 原创微博的frame
    CGFloat originX = 0;
    CGFloat originY = 10;
    CGFloat originW = LGScreenW;
    
    _originalViewFrame = CGRectMake(originX, originY, originW, originH);

    
}

#pragma mark -计算配图尺寸
-(CGSize) setPhotosSizeWithCount:(NSInteger)count
{
    CGFloat photoWH = (LGScreenW-2*LGPhotoMargin-2*LGStatusCellMargin)/3;
    //列数
    int  V_num = count==4? 2 : 3;
    
    //行数 = （图数 - 1）/列数 + 1
    int  H_num = ((int)count-1)/V_num + 1;
    
    CGFloat W = LGScreenW-2*LGStatusCellMargin; //photoWH * V_num + LGStatusCellMargin * (V_num-1);
    CGFloat H = photoWH * H_num + LGPhotoMargin * (H_num-1);
     return CGSizeMake(W, H);
    
}

#pragma mark -  计算转发微博
-(void) setUpRetweetViewFrame
{
    // 昵称frame
    // 昵称
    CGFloat nameX = LGStatusCellMargin;
    CGFloat nameY = nameX;
     _statues.retweeted_status.user.name =[NSString stringWithFormat:@"@%@",_statues.retweeted_status.user.name];
    // 注意：一定要是转发微博的用户昵称
    CGSize nameSize = [_statues.retweeted_status.user.name sizeWithFont:LGNameFont];
    _retSweetNameFrame = (CGRect){{nameX,nameY},nameSize};
    
    
    // 正文
    CGFloat textX = nameX;
    CGFloat textY = CGRectGetMaxY(_retSweetNameFrame) + LGStatusCellMargin;
    
    CGFloat textW = LGScreenW - 2 * LGStatusCellMargin;
    // 注意：一定要是转发微博的正文
    CGSize textSize = [_statues.retweeted_status.text sizeWithFont:LGTextFont constrainedToSize:CGSizeMake(textW, MAXFLOAT)];
    _retSweetContentFrame = (CGRect){{textX,textY},textSize};
    
    //配图
    CGFloat retweetH = CGRectGetMaxY(_retSweetContentFrame) + LGStatusCellMargin;
    NSInteger count = _statues.retweeted_status.pic_urls.count;
    if (count) {
        CGFloat photosViewX = LGStatusCellMargin;
        CGFloat photosViewY = CGRectGetMaxY(_retSweetContentFrame) + LGStatusCellMargin;
        CGSize photoViewSize = [self setPhotosSizeWithCount:count];
        _retSweetPhontosFrame = (CGRect){{photosViewX,photosViewY},photoViewSize};
        retweetH = CGRectGetMaxY(_retSweetPhontosFrame) + LGStatusCellMargin;
    }

    // 原创微博的frame
    CGFloat retweetX = 0;
    CGFloat retweetY = CGRectGetMaxY(_originalViewFrame);
    CGFloat retweetW = LGScreenW;
    _retSweetViewFrame= CGRectMake(retweetX, retweetY, retweetW, retweetH);

}


@end
